<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Drawing;
using System.IO;
using System.Web;
using MosaicManager;
using Fullsix.Common.Configuration;

public class Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        //context.Response.ContentType = "image/jpeg";
        //context.Response.Write("Hello World");

        string foto = context.Request.QueryString["IdUtente"];
        
        //PopolaListaImmagini(context);
        Image img = null;
        //img = Image.FromFile(context.Server.MapPath(string.Format("{0}{1}/{2}",ConfigurationProxy.ReadConfigurationSetting("CartellaImmagini"), Path.GetFileNameWithoutExtension(foto), foto)));
        string partenza = "D:\\progetti\\Fullsix\\Vodafone\\VodafoneMosaico\\wwwroot\\images\\ImageUtente\\795292551\\795292551.jpg";
        string arrivo = "D:\\progetti\\Fullsix\\Vodafone\\VodafoneMosaico\\wwwroot\\images\\ImageUtente\\795292551\\r_795292551.jpg";
        ResizeImageWithAspect(partenza, arrivo, 80);
        img = Image.FromFile("D:\\progetti\\Fullsix\\Vodafone\\VodafoneMosaico\\wwwroot\\images\\ImageUtente\\795292551\\r_795292551.jpg");
        //img = Image.FromFile("D:\\progetti\\Fullsix\\Vodafone\\VodafoneMosaico\\wwwroot\\images\\ImageUtente\\795292551\\795292551.jpg");
        MosaicManager.ManageMosaic mMosaic = new ManageMosaic();
        //string dbImage =  context.Server.MapPath(string.Format("{0}{1}/{1}.idb", ConfigurationProxy.ReadConfigurationSetting("CartellaImmagini"), Path.GetFileNameWithoutExtension(foto)));
        string dbImage = "D:\\progetti\\Fullsix\\Vodafone\\VodafoneMosaico\\wwwroot\\images\\ImageUtente\\795292551\\m_795292551.idb";

        //int altezza = ControlloGrandezza(img.Height, 1000);
        //int larghezza = ControlloGrandezza(img.Width, 1000);
        //Byte[] result = (Byte[])new ImageConverter().ConvertTo(img, typeof(Byte[]));
        //MemoryStream ms = new MemoryStream(result);
        //Image strImg = Image.FromStream(ms);
        //Bitmap imgNew = new Bitmap(strImg, new Size(larghezza, altezza)); 
        
        Image newImg = mMosaic.CreateMosaico(img, dbImage, 40);

        SalvaImmagine(newImg, foto, context);

        context.Response.ContentType = "image/jpeg";

        context.Response.OutputStream.Write(mMosaic.ImageToByte(newImg), 0, mMosaic.ImageToByte(newImg).Length);
        
        
    }

    protected void SalvaImmagine(Image immagine, string foto, HttpContext context)
    { 
        //foto = context.Server.MapPath(string.Format("{0}{1}/m_{1}",ConfigurationProxy.ReadConfigurationSetting("CartellaImmagini"), Path.GetFileNameWithoutExtension(foto)));
        foto = "D:\\progetti\\Fullsix\\Vodafone\\VodafoneMosaico\\wwwroot\\images\\ImageUtente\\795292551\\";

        immagine.Save(string.Format("{0}{1}.jpg", foto, "m_795292551"), System.Drawing.Imaging.ImageFormat.Jpeg);
    }
        

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    protected int ControlloGrandezza(int valoreGrandezza, int confronto)
    {
        while (valoreGrandezza >= confronto)
        {
            valoreGrandezza = valoreGrandezza / 2;
        }
        return valoreGrandezza;
    }


    private void ResizeImageWithAspect(string fileName, string nFileName, int newWidth)
    {
        Image original = Image.FromFile(fileName);
        //Find the aspect ratio between the height and width.
        float aspect = (float)original.Height / (float)original.Width;
        //Calculate the new height using the aspect ratio
        // and the desired new width.
        int newHeight = (int)(newWidth * aspect);
        //Create a bitmap of the correct size.
        //Bitmap temp = new Bitmap(newWidth, newHeight, original.PixelFormat);
        
        //******************************************
        Byte[] result = (Byte[])new ImageConverter().ConvertTo(original, typeof(Byte[]));
        MemoryStream ms = new MemoryStream(result);
        Image strImg = Image.FromStream(ms);
        Bitmap imgNew = new Bitmap(strImg, new Size(newWidth, newHeight)); 
        //******************************************
        
        //Get a Graphics object from the bitmap.
        //Graphics newImage = Graphics.FromImage(temp);
        //Draw the image with the new width/height
        //newImage.DrawImage(original, 0, 0, newWidth, newHeight);
        //Save the bitmap
        imgNew.Save(nFileName); //.Save(outputFileName);
        //Dispose of our objects.
        original.Dispose();
        strImg.Dispose();
        imgNew.Dispose();
    }
}